package com.rlyy.basic.process;

import com.rlyy.basic.mapping.RequestHandlerMapping;
import com.rlyy.common.param.Request;
import com.rlyy.basic.resolver.AbstractParameterParse;
import com.rlyy.common.response.Result;
import com.rlyy.basic.support.InterceptionExecuteChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

/**
 * 处理同步请求
 *
 * @author yuanqinglong
 * @since 2021/9/14 11:34
 */
@Component
public class SyncProcessor implements ISyncAsyncProcessor {

    static final Logger logger = LoggerFactory.getLogger(SyncProcessor.class);

    @Autowired
    private AbstractParameterParse parameterParsing;

    @Autowired
    private RequestHandlerMapping requestHandlerMapping;

    @Autowired
    private InterceptionExecuteChain interceptionExecuteChain;


    /**
     * 同步处理
     *
     * @return boolean
     */
    @Override
    public boolean asyncProcess() {
        return false;
    }

    /**
     * 同步处理过程
     *
     * @param request 完整的请求
     * @return 结果
     */
    @Override
    public Result doProcess(Request request) throws Exception {
        HandlerMethod handlerMapping = requestHandlerMapping.getHandlerMapping(request.getApiId());
        interceptionExecuteChain.beforeExecute(request);
        Object[] parameters = parameterParsing.parsePackageParameters(handlerMapping, request);
        Result result = (Result) handlerMapping.getMethod().invoke(handlerMapping.getBean(), parameters);
        // 反射执行之后处理流程
        interceptionExecuteChain.afterExecute(request, result);
        return result;
    }
}
